import { t } from 'i18next'
import Alert from '@/components/Alert'
import requestMeeting, { request } from '@/plugins/axios'
import { Meeting } from '../meeting'
import { Mt, MtEntity } from '../mt'
import { makeMtEntityParam, makeMtParam } from '../type'
import {
	FetchRollInfoEntity,
	FetchRollSetCallee,
	FetchSearchAddCalleeEntity,
	FetchSearchCalleeEntity,
	FetchStartRollCallEntity,
	PlanDetailEntity,
	SelectViewParamsEntity,
} from './interface'
/**
 *
 * @param meeting_id 会议的meetingMoid
 * @returns 获取轮询信息
 */
export const fetchGetRollInfo = async (meetingMoid: string) =>
	requestMeeting<FetchRollInfoEntity>({
		url: `/mcc/${meetingMoid}/roll_call`,
		method: 'get',
	}).then((res) => res)
/**
 *
 * @param meetingMoid
 * @returns 获取预案列表数据
 */
export const fetchGetRollPlanList = async (meetingMoid: string) =>
	requestMeeting<[{ id: string; name: string }]>({
		url: `/meetings/${meetingMoid}/prepolls?count=0`,
		method: 'GET',
	}).then((res) => res)
/**
 *
 * @param meetingMoid 会议的meetingMoid
 * @param value 预案名称
 * @returns 删除预案
 */
export const fetchDeleteRollPlan = async (meetingMoid: string, value: string) =>
	requestMeeting({
		url: `/meetings/${meetingMoid}/prepolls/${value}`,
		method: 'DELETE',
	}).then((res) => res)
/**
 *
 * @param meetingMoid 会议的meetingMoid
 * @param value 预案名称
 * @returns 新增预案
 */
export const fetchAddRollPlan = async (meetingMoid: string, plan_data: any) =>
	requestMeeting({
		url: `/meetings/${meetingMoid}/prepolls`,
		method: 'POST',
		data: JSON.stringify(plan_data),
	}).then((res) => res)
/**
 *
 * @param meetingMoid 会议的meetingMoid
 * @param value 预案名称
 * @returns 更新预案
 */
export const fetchUpdatePollPlan = async (meetingMoid: string, id: string, plan_data: any) =>
	requestMeeting({
		url: `/meetings/${meetingMoid}/prepolls/${id}`,
		method: 'POST',
		data: JSON.stringify(plan_data),
	}).then((res) => res)
/**
 *
 * @param meetingMoid 会议的meetingMoid
 * @param value 预案的id
 * @returns  获取预案详情
 */
export const fetchGetRollPlanDetail = async (meetingMoid: string, value: string) =>
	request<PlanDetailEntity>({
		url: `/meetings/${meetingMoid}/prepolls/${value}?type=1`,
		method: 'GET',
	}).then((res) => res)
/**
 * @param data 设置点名人信息(开始前点名)
 */
export const fetchSaveRollCallBeforeStar = async (
	meetingMoid: string,
	data: {
		/**点名模式 */
		mode: string
		/**预案名称*/
		name: string
		/**点名人信息 */
		roll_caller?: any
	}
) =>
	request({
		url: `/mcc/${meetingMoid}/roll_call/caller`,
		method: 'POST',
		data: data,
	}).then((res) => res)
/**
 * 被点名人搜索
 */
export const fetchSearchCallee = (meetingMoid: string, params: { key: string }) =>
	requestMeeting<FetchSearchCalleeEntity>({
		url: `/mcc/${meetingMoid}/roll_call/search`,
		method: 'GET',
		params: params,
	}).then((res) => res)
/**
 * @param meetingMoid
 * @returns 检测点名会议，轮询是否开启
 */
export const fetchRollCallCheck = async (meetingMoid: string) =>
	requestMeeting<{
		/**管理方轮询是否开启 */
		poll_ma_is_on: 0 | 1
		/**轮询是否开启 */
		poll_is_on: 0 | 1
	}>({
		url: `/mcc/${meetingMoid}/roll_call/check`,
		method: 'GET',
	}).then((res) => res)
/**
 * @param MeetingMoid
 * @param data:{name,mode,roll_caller}
 */
export const fetchStartRollCall = async (meetingMoid: string, data: FetchStartRollCallEntity) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/roll_call`,
		method: 'POST',
		data: data,
	}).then((res) => res)
/**点名终端*/
export const fetchRollCaller = async (meetingMoid: string, data: FetchRollSetCallee) =>
	request<{ data: string }>({
		url: `/mcc/${meetingMoid}/roll_call/caller`,
		method: 'PUT',
		data: data,
	}).then((res) => res)
/** 指定会议发言人&设置终端强制广播*/
export const fetchRollSetSpeaker = async (meeting_moid: string, mt: any) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/speaker`,
		method: 'PUT',
		data: {
			mt: mt instanceof Meeting || mt instanceof Mt ? makeMtParam(mt) : makeMtEntityParam(mt),
		},
	}).then((res) => res)
/** 设置管理方*/
export const fetchRollSetChairman = async (meeting_moid: string, mt: any) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/chairman`,
		method: 'PUT',
		data: mt instanceof Mt ? makeMtParam(mt) : makeMtEntityParam(mt),
	}).then((res) => res)
/** 取消管理方*/
export const fetchRollCancelChairman = async (meeting_moid: string, mt: any) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/chairman`,
		method: 'DELETE',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('取消管理方失败'))
			} else {
				return res
			}
		})
		.catch(() => {
			Alert(t('取消管理方失败'))
		})
export const fetchRollCallCallee = (meeting_moid: string, searchType: number, data: any) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/roll_call/callees?is_search=` + searchType,
		method: 'POST',
		data: data,
	})
		.then((t) => {
			return t
		})
		.catch(() => {
			Alert(t('操作失败'))
		})
/**设置点名结果 */
export const fetchSetRollcallResult = (meeting_moid: string, data: any) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/roll_call/result`,
		method: 'POST',
		data: data,
	}).then((res) => res)

/**备注保存 */
export const fetchRemarkSave = async (meetingMoid: string, params: { remark: string; roll_callee: MtEntity }) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/roll_call/remark`,
		method: 'POST',
		data: params,
	}).then((res) => res)

export const fetchAddRollCallCallee = (meeting_moid: string, data: any) =>
	requestMeeting<FetchSearchAddCalleeEntity>({
		url: `/mcc/${meeting_moid}/roll_call/callees?is_search=1`,
		method: 'POST',
		data: data,
	}).then((res) => res)

export const fetchSaveRollcalleeList = (meeting_moid: string, data: any) =>
	requestMeeting<FetchSearchAddCalleeEntity>({
		url: `/mcc/${meeting_moid}/roll_call/callees?is_search=0`,
		method: 'POST',
		data: data,
	}).then((res) => res)

/**删除被点名列表 */
export const fetchDelRollcalleeList = (meeting_moid: string, data: any) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/roll_call/del`,
		method: 'POST',
		data: data,
	}).then((res) => res)

/**停止点名轮询*/
export const fetchStopRoll = (meeting_moid: string) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/roll_call`,
		method: 'DELETE',
	}).then((res) => res)

/**点名人选看被点名人 */
export const fetchStartSelectView = async (meeting_id: string, params: SelectViewParamsEntity) =>
	requestMeeting({
		url: `/mcc/${meeting_id}/select_view`,
		method: 'POST',
		data: params,
	}).then((res) => res)
